/// describe
///
/// created by hujintao
/// created at 2021/5/19
//
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterappdriver/utils/toast.dart';
import 'package:flutterappdriver/utils/utils.dart';
import 'package:oktoast/oktoast.dart';

class ContactDetailPage extends StatefulWidget {
  final String title;
  List? phone;

  ContactDetailPage({Key? key, this.title = "联系人", this.phone})
      : super(key: key);

  @override
  _ContactDetailPageState createState() => _ContactDetailPageState();
}

class _ContactDetailPageState extends State<ContactDetailPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (this.widget.phone != null) {
    } else {
      this.widget.phone = ["19367151969"];
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('YouFriend：${this.widget.title}'),
      ),
      body: _buildUI(),
    );
  }

  Widget _buildUI() {
    return SafeArea(
        child: SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.all(20),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ClipOval(
                child: Image.asset(
              "./assets/images/avatar.png",
              width: 80.0,
            )),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                "${this.widget.title}",
                textScaleFactor: 1.2,
              ),
            ),

            this.widget.phone!.length > 0
                ? createList()
                : Center(
                    child: Text("暂无手机号~"),
                  )
            // ,
          ],
        ),
      ),
    ));
  }

  Widget createList() {
    return ListView.builder(
      itemBuilder: (context, index) {
        return new Card(
            child: new ListTile(
          leading: GestureDetector(
            child: new Icon(Icons.phone),
            onTap: () {
              Utils().makePhoneCall(phone: "${this.widget.phone?[index]}");
            },
          ),
          title: new Text("${this.widget.title}: $index"),
          subtitle: new Text("${this.widget.phone?[index]}"),
          trailing: GestureDetector(
            child: new Icon(Icons.copy_outlined),
            onTap: () {
              this.copyEvent(text: "${this.widget.phone?[index]}");
            },
          ),
          contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
          enabled: true,
          onTap: (){
            Utils().makePhoneCall(phone: "${this.widget.phone?[index]}");
          },
          onLongPress: () => print("$index被长按了"),
        ));
      },
      shrinkWrap: true,
      itemCount: this.widget.phone?.length ?? 0,
    );
  }


  copyEvent ({String text = ""}){
    Clipboard.setData(ClipboardData(text: text));

    Toast.show('复制$text成功');
  }

}
